import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class AssetUtil {
  AssetUtil._();

  static Widget svgImage(String path,
      {String? package, Size? size, Color? color, BoxFit fit = BoxFit.contain, ColorFilter? colorFilter}) {
    return SvgPicture.asset(
      _realPath(path, 'svg'),
      // color: color,
      colorFilter: colorFilter ?? ColorFilter.mode(color ?? Colors.transparent, BlendMode.srcATop),
      width: size?.width,
      height: size?.height,
      package: package,
      fit: fit,
    );
  }

  static Widget pngImage(String path,
      {String? package, Size? size, BoxFit fit = BoxFit.contain, Color? color, bool gaplessPlayback = false}) {
    return Image.asset(
      _realPath(path, 'png'),
      package: package,
      width: size?.width,
      height: size?.height,
      fit: fit,
      color: color,
      gaplessPlayback: gaplessPlayback,
    );
  }

  static Widget webpImage(String path, {String? package, Size? size, BoxFit fit = BoxFit.contain, Color? color}) {
    return Image.asset(
      _realPath(path, 'webp'),
      package: package,
      width: size?.width,
      height: size?.height,
      fit: fit,
      color: color,
    );
  }

  static Widget rawImage(String path, {String? package, Size? size, BoxFit fit = BoxFit.contain, Color? color}) {
    return Image.asset(
      path,
      package: package,
      width: size?.width,
      height: size?.height,
      fit: fit,
      color: color,
    );
  }

  static Widget gifImage(String path, {String? package, Size? size, BoxFit fit = BoxFit.contain}) {
    return Image.asset(
      _realPath(path, 'gif'),
      package: package,
      width: size?.width,
      height: size?.height,
      fit: fit,
    );
  }

  static String _realPath(String path, String lowCaseSuffix) {
    if (path.length < lowCaseSuffix.length) {
      return path;
    }

    String realPath = "";
    String lastWords = path.substring(path.length - lowCaseSuffix.length);
    if (lastWords.toLowerCase() == lowCaseSuffix) {
      return path;
    }

    return "$path.$lowCaseSuffix";
  }
}
